<?php

namespace app\common\model\customer;

use app\api\controller\Pub;
use app\common\model\company\Company;
use app\common\model\company\Contract;
use app\common\model\customer\Customervisit;
use app\common\model\customer\Street;
use app\common\model\power\Rolepower;
use app\common\model\user\User;
use app\common\model\apiinput\Apiyunkefriends;
use app\common\model\apiinput\Apiyunkerecords;
use think\Model;

class Customer extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'customer';
    protected $pk    = 'CustomerId';

    const STATEINT    = 1; //初始化 目标客户
    const STATEINT2   = 11; //重点目标客户
    const STATEWAIT   = 2; //跟进中客户
    const STATEWAIT2  = 12; //重点跟进客户
    const STATEOK     = 99; //没问题的 已成交客户
    const STATEDELETE = 404; //无效客户

    public function companyinfo()
    {
        return $this->hasOne('app\common\model\company\Company', 'CompanyId', 'CompanyId');
    }

    public function bussinfo()
    {
        return $this->hasOne('app\common\model\user\User', 'UserId', 'BussinessId');
    }

    public function contractlist()
    {
        return $this->hasOne('app\common\model\company\Contract', 'CompanyId', 'CompanyId');
    }

    /**
     * 状态分类
     * @param  string $value [description]
     * @return [type]        [description]
     */
    public static function getStatusText($value = '')
    {
        $status = [-1 => '全部客户', 3 => '一般客户', 1 => '目标客户', 11 => '重点目标客户', 2 => '跟进中', 12 => '重点跟进', 99 => '已成交', 404 => '无效'];
        if ($value !== '') {
            return $status[$value];
        }
        return $status;
    }

    /**
     * 创建企业信息
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function user_create($data)
    {
        $userid   = Pub::userid();
        $Customer = new Customer();

        $Customer->AddTime = date('Y-m-d H:i:s', time());

        //自动录入
        $Customer->Name          = $data['Name'];
        $Customer->Address       = $data['Address'];
        $Customer->Lon           = $data['lon'];
        $Customer->Lat           = $data['lat'];
        $Customer->Tel           = $data['Tel'];
        $Customer->OtherTel      = $data['OtherTel'];
        $Customer->LicenseNumber = $data['LicenseNumber'];

        //手工录入
        $Customer->UserName = $data['UserName'];
        $Customer->UserTel  = $data['UserTel'];
        $Customer->Type     = $data['Type'];
        $Customer->Scale    = $data['Scale'];
        $Customer->Note     = $data['Note'];
        $Customer->Img      = $data['Img'];

        //跟单信息
        // $Customer->LastTime        = date('Y-m-d H:i:s', time());
        // $Customer->AppointmentTime = '';
        // $Customer->Visitingtime    = '';
        $Customer->BindTime    = date('Y-m-d H:i:s', time());
        $Customer->BussinessId = $userid;

        if (!empty($data['CompanyId'])) {
            $Customer->CompanyId = $data['CompanyId'];
        }

        if (!empty($data['Source'])) {
            $Customer->Source = $data['Source'];
        }

        //客户关系-上级代理商
        if (!empty($data['AgentId'])) {
            $Customer->AgentId = $data['AgentId'];
        }

        //客户所属街道
        if (get_config_value('input_street_area') == 'must' && empty($data['StreetId'])) {
            return moerr('请选择所属街道');
        }

        if (!empty($data['StreetId'])) {
            $Customer->StreetId = $data['StreetId'];
        }

        if (!empty($data['State'])) {
            $Customer->State = $data['State'];
        } else {
            $Customer->State = Customer::STATEINT;
        }

        //是否为代理商
        if (!empty($data['IsAgent'])) {
            $Customer->IsAgent = $data['IsAgent'];
        } else {
            $Customer->IsAgent = 0;
        }

        $Customer->save();
        return $Customer->CustomerId;
    }
    /**
     * 认领客户
     * @return [type] [description]
     */
    public static function claim($customerid, $userid, $customer = '')
    {
        if (empty($customer)) {
            $customer = Customer::get($customerid);
            if (empty($customer)) {
                return moerr('没有找到客户信息');
            }
        }
        if ($customer->guard_state && !Rolepower::ispower(204007)) {
            return moerr('客户处于保护期，无法认领');
        }
        if ($customer->BussinessId == $userid) {
            return moerr('目前客户负责人是您，无需修改');
        }
        //增加客户转移记录
        $allocationlog['CId']            = $customerid;
        $allocationlog['BussinessId']    = $userid;
        $allocationlog['OldBussinessId'] = $customer['BussinessId'];

        //修改客户信息
        $customer->BussinessId = $userid;
        $customer->save();

        //插入修改记录
        $AllocationLog = new AllocationLog();
        $AllocationLog->addAllocationLog($allocationlog, $userid);

        //修改企业表客户负责人
        if (!empty($customer['CompanyId'])) {
            $company              = Company::get($customer['CompanyId']);
            $company->BussinessId = $userid;
            $company->save();
        }
        return sok('认领成功');
    }

    /**
     * 其他电话
     * @param  [type] $value [description]
     * @return [type]        [description]
     */
    public static function getOtherTelAttr($value)
    {
        if (!empty($value)) {
            if (strstr($value, ',') !== false) {
                return explode(',', $value);
            } else if (strstr($value, ';') !== false) {
                return explode(';', $value);
            } else {
                return [$value];
            }
        } else {
            return [];
        }
    }
    /**
     * 测试是否能通过客户守卫
     * @param  [type] $id      [description]
     * @param  [type] $user_id [description]
     * @return [type]          [description]
     */
    public static function run_guard($id, $user_id = '', $info = '', $is_goback = true)
    {
        if (empty($user_id)) {
            $user_id = Pub::userid();
        }
        if (empty($info)) {
            $info = Customer::get($id);
        }
        if ($info->guard_state && $info->BussinessId !== $user_id && !Rolepower::ispower(204007)) {
            if ($is_goback) {
                return moerr('没有权限', '', 'goback');
            } else {
                return serr('没有权限');
            }
        }
    }
    /**
     * 保护期守卫
     * 如果在保护期返回true 不在保护期返回false
     * @param  [type] $value [description]
     * @param  [type] $data  [description]
     * @return [type]        [description]
     */
    public static function getGuardStateAttr($value, $data)
    {
        //开启 用户保护期
        if (CUSTOMER_BIND_SWITCH && !empty($data['BussinessId'])) {
            if (!empty($data['AddTime'])) {
                $time = strtotime($data['AddTime']);
                //超过保护期 返回假
                if ($time + (3600 * 24 * CUSTOMER_BIND_DAY) > time()) {
                    return true;
                }
            }
            //判断沟通时间
            $sb = CustomerVisit::where('CustomerId', $data['CustomerId'])->where('BussinessId', $data['BussinessId'])->order('AddTime desc')->find();
            if (!empty($sb['AddTime'])) {
                $time = strtotime($sb['AddTime']);
                //超过保护期 返回假
                if ($time + (3600 * 24 * CUSTOMER_BIND_DAY) > time()) {
                    return true;
                }
            }

            //检测合同到期后____天，内属于保护期
            if (!empty($data['CompanyId'])) {
                $contract_endtime = Contract::where('CompanyId', $data['CompanyId'])->where('State', Contract::STATEOK)->order('EndTime desc')->value('EndTime');
                if (!empty($contract_endtime)) {
                    $time = strtotime($contract_endtime);
                    //超过保护期 返回假
                    if ($time + (3600 * 24 * CUSTOMER_BIND_DAY) > time()) {
                        return true;
                    }
                }
            }
            //在保护期
            return false;
        }
        return false;
    }

    /**
     * 获取数组内的守卫状态
     * @param  [type] $data   [数组]
     * @param  [type] $test   [是否判断属于此业务员的,如果是 则提示非保护期 否则为保护期]
     * @param  [type] $bussid [业务员id]
     * @return [type]         [guard_state：true为保护  false为不保护]
     */
    public static function getGuardStateList($data, $test = true, $bussid = '')
    {
        //开启 用户保护期
        if (CUSTOMER_BIND_SWITCH) {
            $todaytime  = time(); //获取当前时间戳
            $formatearr = []; //格式化数组 只为了查询方便

            // 首次检测
            foreach ($data as $key => $value) {
                $data[$key]['guard_state'] = true; //默认为保护

                //如果业务员为空 客户不保护
                if (empty($value['BussinessId'])) {
                    $data[$key]['guard_state'] = false; //不保护
                } else if ($test) {
                    if (empty($bussid)) {
                        $bussid = Pub::userid();
                    }
                    if ($value['BussinessId'] == $bussid) {
                        $data[$key]['guard_state'] = false; //不保护
                    }
                }

                //如果目前判断为保护状态，则进行添加时间在范围判断
                //添加时间 在保护期内保护 则保护 否则不保护
                if ($data[$key]['guard_state'] == true) {
//需要进一步判断是否在保护期内
                    $add = false;
                    if (!empty($value['AddTime'])) {
                        $time = strtotime($value['AddTime']);
                        //超过保护期 则需要进一步判断
                        if (($time + (3600 * 24 * CUSTOMER_BIND_DAY)) < $todaytime) {
//错过了保护期
                            $add = true;
                        } //没有错过保护期的直接跳过
                    } else {
                        //没有添加时间的默认为在保护期内，进行进一步判断
                        $add = true;
                    }
                    if ($add) {
//是否需要进一步判断
                        //存储需要查询的回访记录
                        $customerid[]                        = $value['CustomerId'];
                        $customerkeyid[$value['CustomerId']] = $value['CustomerId'];
                        //存储后续需要操作的数据
                        $formatearr[$value['CustomerId']] = array_merge($value, ['arr_key' => $key]);
                    }
                }
                // dump($data[$key]);
            }
            // dump($customerid);
            //判断回访记录 - 筛选第一批客户回访记录
            $customerid_contract  = []; //需要 判断是否存在生效的合同 企业id对应的客户id表
            $customerid_companyid = []; //需要判断是否存在生效的合同  企业id表

            $customerid_visit = []; //需要 查询（单条查询）的用户回访记录
            if (!empty($customerid)) {
                //查询回访记录
                $visitlist = CustomerVisit::where('CustomerId', 'in', $customerid)->group('CustomerId')->field('BussinessId,CustomerId,max(AddTime) as AddTime')->select();
                foreach ($visitlist as $key => $value) {
                    //检测到非此绑定业务员的回访记录
                    if ($value['BussinessId'] !== $formatearr[$value['CustomerId']]['BussinessId']) {
                        if (empty($formatearr[$value['CustomerId']]['CompanyId'])) {
                            //需要单独查询回访表
                            $customerid_visit[] = $value['CustomerId'];
                        } else {
                            //可以查询合同表
                            $customerid_contract[$formatearr[$value['CustomerId']]['CompanyId']] = $value['CustomerId'];
                            $customerid_companyid[]                                              = $formatearr[$value['CustomerId']]['CompanyId'];
                        }
                        continue;
                    }
                    //判断回访记录
                    if (!empty($value['AddTime'])) {
                        $time = strtotime($value['AddTime']);
                        //超过保护期 返回假
                        if ($time + (3600 * 24 * CUSTOMER_BIND_DAY) < $todaytime) {
                            $data[$formatearr[$value['CustomerId']]['arr_key']]['guard_state'] = false;
                        }
                    }
                    unset($customerkeyid[$value['CustomerId']]);
                }
                //将没有回访记录的客户信息添加到合同表再次查询
                foreach ($customerkeyid as $key => $value) {
                    if (!empty($formatearr[$value]['CompanyId'])) {
                        $customerid_contract[$formatearr[$value]['CompanyId']] = $value;
                        $customerid_companyid[]                                = $formatearr[$value]['CompanyId'];
                    } else {
                        $data[$formatearr[$value]['arr_key']]['guard_state'] = false;
                    }

                }
            }

            //是否存在生效的合同 检测合同到期后____天，内属于保护期
            if (!empty($customerid_companyid)) {
                $contract_endtime = Contract::where('CompanyId', 'in', $customerid_companyid)->where('State', Contract::STATEOK)->group('CompanyId')->field('CompanyId,max(EndTime) as EndTime')->select();
                if (!empty($contract_endtime)) {
                    foreach ($contract_endtime as $key => $value) {
                        $time = strtotime($value['EndTime']);
                        //超过保护期 返回假
                        if ($time + (3600 * 24 * CUSTOMER_BIND_DAY) < $todaytime) {
                            $data[$formatearr[$customerid_contract[$value['CompanyId']]]['arr_key']]['guard_state'] = false;
                        }
                        //去除此检测完毕的客户id
                        unset($customerid_contract[$value['CompanyId']]);
                    }
                }
                //将没有找到合同的企业退回给回访表，在去逐个查询
                if (!empty($customerid_contract)) {
                    foreach ($customerid_contract as $key => $value) {
                        $customerid_visit[] = $value;
                    }
                }
            }

            //将非本人的回访记录二次筛选
            if (!empty($customerid_visit)) {
                foreach ($customerid_visit as $key => $value) {
                    $customer = $formatearr[$value];
                    $AddTime  = CustomerVisit::where('CustomerId', $customer['CustomerId'])->where('BussinessId', $customer['BussinessId'])->order('AddTime desc')->value('AddTime');
                    $time     = strtotime($AddTime);
                    //超过保护期 返回假
                    if ($time + (3600 * 24 * CUSTOMER_BIND_DAY) < $todaytime) {
                        $data[$customer['arr_key']]['guard_state'] = false;
                    }
                }
            }
        } else {
            //全部赋值为不保护
            foreach ($data as $key => $value) {
                $data[$key]['guard_state'] = false;
            }
        }
        return $data;
    }

    /**
     * 获取认领状态
     * true 可认领 false 不可认领
     * @return [type] [description]
     */
    public static function getHasClaim($data)
    {
        if ($data->guard_state == false && $data->BussinessId !== Pub::userid()) {
            return true;
        } else {
            return false;
        }
    }

/**
 * 获取信息
 * @param  [type] $id [description]
 * @return [type]     [description]
 */
    public static function user_getinfo($id, $type = 'buss', $hidden = ['OperationLog'])
    {
        $data = Customer::get($id);
        if (empty($data)) {
            return moerr('没有找到您需要的信息');
        }
        $data['list'] = Customervisit::getlist($id);
        if ($type !== 'datascreen') {
            $data['guard_state'] = $data->guard_state;
            $data['has_claim']   = Customer::getHasClaim($data);
        }
        //展示客户项目
        $isshow_project  = true;
        $data['project'] = [];
        //保护期 守卫
        if ($type == 'buss') {
            if ($data->guard_state && ($data->BussinessId !== Pub::userid() && !Rolepower::ispower(204007))) {
                if (!CUSTOMER_BIND_SHOW_INFO) {
                    return moerr('该客户处于保护期,无法查看', '', 'goback');
                }
                //保护期客户将隐藏客户的联系方式\其他电话\负责人电话\负责人姓名\备注\图片备注\
                $data['Tel']      = substr($data['Tel'], 0, 2) . '***' . substr($data['Tel'], -3);
                $data['OtherTel'] = [];
                $data['UserName'] = '该客户处于保护期,无法查看';
                $data['UserTel']  = substr($data['UserTel'], 0, 2) . '***' . substr($data['UserTel'], -3);
                $data['Note']     = '该客户处于保护期,无法查看';
                $data['Img']      = '';
                $data['list']     = [];
                $isshow_project   = false;
            }
        } else if ($type == 'mana') {
            $hidden = [];
        } else if ($type == 'datascreen') {
            $hidden = ['OperationLog'];
        }
        //将客户档案加载入系统
        if ($isshow_project) {
            $data['project'] = CustomerProject::getlist($id);
            //将客户上级关系加入系统
            if (!empty($data['AgentId'])) {
                $data['agent'] = Customer::get($data['AgentId'])->visible(['Name', 'CustomerId', 'Address', 'UserName']);
            }
            $data['agent_customer'] = Customer::where('AgentId', $id)->visible(['Name', 'CustomerId', 'Address', 'UserName'])->select();
        }

        $data['StateText'] = Customer::getStatusText($data['State']);
        $data['StateList'] = Customer::getStatusText();
        $data['ScaleList'] = array_del_empty(Customer::distinct(true)->column('Scale'));

        $data['TypeList'] = array_del_empty(Customer::distinct(true)->column('Type'));

        if (!empty($data['BussinessId'])) {
            $data['bussinfo'] = User::getinfo('', $data['BussinessId'])->visible(['Name', 'Iphone']);
            if(!empty($data['bussinfo'])){
               $data['bussinfo'] =$data['bussinfo']->toArray();  
            }
        }
        $data->hidden($hidden);

        //客户所属区域
        if (!empty($data['StreetId'])) {
            $data['street'] = Street::getinfo($data['StreetId']);
        }

        return $data;
    }

/**
 * 获取订单列表
 * @param  [type] $data    [主数据]
 * @param  string $type    [查询类型/hawa/buss]
 * @param  [type] $is_page [是否分页显示]
 * @return [type]          [description]
 */
    public static function usergetlist($data, $type = 'buss', $is_page = true)
    {
        $rdata = new Customer;
        if ($type == 'buss') {
            // $rdata   = Customer::where('State', Customer::STATEOK);
            $rdata   = $rdata->with(['bussinfo']);
            $visible = ['CustomerId', 'AddTime', 'CompanyId', 'Name', 'LastTime', 'UserName', 'UserTel', 'Address', 'Lon', 'Lat', 'Type', 'Scale', 'State', 'LastTime', 'BussinessId', 'VisitCount', 'Note'];
        } else if ($type == 'mana') {
            // $rdata   = Customer::where('State', Customer::STATEOK);
            $rdata   = $rdata->with(['bussinfo']);
            $visible = ['CustomerId', 'AddTime', 'CompanyId', 'Name', 'LastTime', 'UserName', 'UserTel', 'Address', 'Lon', 'Lat', 'Type', 'Scale', 'State', 'LastTime', 'BussinessId', 'VisitCount', 'Note'];
        } else if ($type == 'datascreen') {
            $rdata = $rdata->with(['bussinfo', 'contractlist' => function ($query) {
                $query->where('State', Contract::STATEOK)->where('EndTime', '>=', date('Y-m-d H:i:s', time()));
                $query->field('CompanyId');
            }]);
            $visible = ['CustomerId', 'AddTime', 'CompanyId', 'Name', 'LastTime', 'UserName', 'UserTel', 'Address', 'Lon', 'Lat', 'Type', 'Scale', 'State', 'LastTime', 'BussinessId', 'VisitCount', 'State'];
        } else if ($type == 'regulatory') {
            $rdata   = $rdata->with(['bussinfo']);
            $visible = ['CustomerId', 'AddTime', 'CompanyId', 'Name', 'LastTime', 'UserName', 'UserTel', 'Address', 'Lon', 'Lat', 'Type', 'Scale', 'State', 'LastTime', 'BussinessId', 'VisitCount'];
        } else {
            return moerr('缺少参数');
        }
        if (!empty($data['field'])) {
            $visible = array_merge($data['field'], $visible);
        }
        $option = come_model_option($data, $rdata, $is_page);

        $rdata              = $option['rdata'];
        $data               = $option['data'];
        $ndata['listcount'] = $option['count'] > 999 ? '999+' : $option['count'];

        if (!empty($data['field'])) {
            $rdata = $rdata->field($visible);
        }

        $rdata = $rdata->select();

        foreach ($visible as $key => $value) {
            if (strstr($value, 'distance')) {
                $visible = array_merge($visible, ['distance']);
            }
        }

        if ($type == 'mana' || $type == 'buss' || $type == 'datascreen' || $type == 'regulatory') {
            $rdata = $rdata->visible(array_merge($visible, ['bussinfo.Name', 'bussinfo.Iphone']));
        }

        $ndata['list'] = $rdata->toArray();
        if ($type == 'datascreen') {
            $ndata['list'] = Customer::getGuardStateList($ndata['list'], false);
        } else {
            $ndata['list'] = Customer::getGuardStateList($ndata['list']);
        }
        $ndata['statetext'] = Customer::getStatusText();
        if ($is_page) {
            $ndata['pageinfo'] = ['pageindex' => $data['pageindex'], 'pagemax' => $data['count']];
        }
        return $ndata;
    }

    /**
     * 用户修改信息
     * @param  [type]  $data   [description]
     * @param  boolean $wtnote [是否记录]
     * @return [type]          [description]
     */
    public static function user_reinfo($data, $wtnote = true)
    {
        if (empty($data['id'])) {
            return moerr('缺少重要参数');
        }
        $info     = Customer::get($data['id']);
        $userinfo = User::getinfo();
        if ($info->guard_state && $info->BussinessId !== Pub::userid() && !Rolepower::ispower(204007)) {
            return moerr('该客户处于保护期,无法修改', '', 'goback');
        }

        $reinfo  = empty($info->OperationLog) ? '' : $info->OperationLog . "\n";
        $reinfo  = $reinfo . '【' . $userinfo->Name . '|' . $userinfo->UserId . '】在' . date('Y-m-d H:i', time()) . '进行了信息调整：';
        $nreinfo = '';
        foreach ($data as $key => $value) {
            $sb = $info->getfiledname($key);
            if (empty($sb)) {
                continue;
            }
            //取原数据
            if ($key == 'OtherTel') {
                if (is_array($info->$key)) {
                    $ovalue = implode(',', $info->$key);
                } else {
                    $ovalue = $info->$key;
                }
            } else {
                $ovalue = $info->$key;
            }

            if ($key == 'State' && $value !== $ovalue) {
                if ((int) $ovalue !== (int) $value) {
                    $info->$key = $value;
                    //value将改变成文字所以提前赋值
                    $value   = $info->getStatusText((int) $value);
                    $ovalue  = $info->getStatusText((int) $ovalue);
                    $nreinfo = $nreinfo . '字段【' . $info->getfiledname($key) . '】由【' . $ovalue . '】修改为【' . $value . '】;' . "\n";
                }
            }if ($key == 'State' && $value !== $ovalue) {
                if ((int) $ovalue !== (int) $value) {
                    $info->$key = $value;
                    //value将改变成文字所以提前赋值
                    $value   = $info->getStatusText((int) $value);
                    $ovalue  = $info->getStatusText((int) $ovalue);
                    $nreinfo = $nreinfo . '字段【' . $info->getfiledname($key) . '】由【' . $ovalue . '】修改为【' . $value . '】;' . "\n";
                }
            } else if ($key == 'isPlanform') {
                if ($value !== $ovalue && (int) $ovalue !== (int) $value) {
                    $info->$key = $value;
                    $nreinfo    = $nreinfo . '字段【' . $info->getfiledname($key) . '】由【' . $ovalue . '】修改为【' . $value . '】;' . "\n";
                }
            } else if ($key == 'IsAgent') {
                if ($value !== $ovalue && (int) $ovalue !== (int) $value) {
                    $info->$key = $value;
                    $value      = empty($value) ? '不是代理商客户' : '代理商客户';
                    $ovalue     = empty($ovalue) ? '不是代理商客户' : '代理商客户';
                    $nreinfo    = $nreinfo . '字段【' . $info->getfiledname($key) . '】由【' . $ovalue . '】修改为【' . $value . '】;' . "\n";
                }
            } else if ($key == 'BussinessId' && $value !== $ovalue) {
                $info->$key = $value;
                //value将改变成文字所以提前赋值
                $value   = $value . User::get($value)->Name;
                $ovalue  = $ovalue . User::get($ovalue)->Name;
                $nreinfo = $nreinfo . '字段【' . $info->getfiledname($key) . '】由【' . $ovalue . '】修改为【' . $value . '】;' . "\n";
            } else if ($key == 'AgentId' && $value !== $ovalue) {
                if ($value == 'clear') {
                    $value = null;
                }
                $info->$key = $value;
                //value将改变成文字所以提前赋值
                $value   = empty($value) ? '暂无' : $value . Customer::get($value)->Name;
                $ovalue  = empty($ovalue) ? '暂无' : $ovalue . Customer::get($ovalue)->Name;
                $nreinfo = $nreinfo . '字段【' . $info->getfiledname($key) . '】由【' . $ovalue . '】修改为【' . $value . '】;' . "\n";
            } else if ($key == 'CompanyId' && $value !== $ovalue) {
                $info->$key = $value;
                //value将改变成文字所以提前赋值
                $value   = $value . Company::get($value)->Name;
                $ovalue  = $ovalue . Company::get($ovalue)->Name;
                $nreinfo = $nreinfo . '字段【' . $info->getfiledname($key) . '】由【' . $ovalue . '】修改为【' . $value . '】;' . "\n";
            } else if (!empty($value) && $ovalue !== $value) {
                $nreinfo    = $nreinfo . '字段【' . $info->getfiledname($key) . '】由【' . $ovalue . '】修改为【' . $value . '】;' . "\n";
                $info->$key = $value;
            }
        }
        if (!empty($nreinfo)) {
            if ($wtnote) {
                $reinfo             = $reinfo . "\n" . $nreinfo;
                $info->OperationLog = $reinfo;
            }
            $info->save();
        } else {
            return moerr('没有找到需要调整的数据');
        }
    }

    public static function getfiledname($str)
    {
        $filename = [
            'State'           => '状态',
            'CompanyId'       => '企业id',
            'Name'            => '企业名称',
            'Address'         => '企业地址',
            'Lon'             => '经度',
            'Lat'             => '纬度',
            'Tel'             => '联系电话',
            'OtherTel'        => '其他电话',
            'LicenseNumber'   => '信用代码',
            'UserName'        => '负责人姓名',
            'UserTel'         => '负责人电话',
            'Type'            => '企业分类',
            'Scale'           => '产废规模',
            'Note'            => '备注',
            'Img'             => '图片备注',
            'AppointmentTime' => '预约时间',
            'Visitingtime'    => '上门时间',
            'BindTime'        => '绑定时间',
            'BussinessId'     => '业务id',
            'isPlanform'      => '是否接入平台',
            'IsAgent'         => '是否为代理商客户',
            'AgentId'         => '上级代理商',
            'Source'          => '客户来源',
            'StreetId'        => '所属街道',
        ];
        if (empty($filename[$str])) {
            return false;
        }
        return $filename[$str];
    }

    /**
     * 获取代理商列表
     * 管理员可获得所有为代理商客户的列表
     * 业务员只能获取所属为自己客户的代理商列表
     * @return [type] [description]
     */
    public static function getagentlist($id = '')
    {
        $customer = Customer::where('IsAgent', 1);
        if (!Rolepower::ispower(204007)) {
            //非管理员筛选
            $customer = $customer->where('BussinessId', Pub::userid());
        }
        if (!empty($id)) {
            $customer = $customer->where('CustomerId', '<>', $id);
        }
        $customer = $customer->order('Name asc')
            ->visible(['Name', 'CustomerId', 'UserName'])->select();
        return $customer;
    }
}
